package com.zadtek.shopmall.fragments;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.goyourfly.multi_picture.ImageLoader;
import com.goyourfly.multi_picture.MultiPictureView;
import com.zadtek.shopmall.App;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsFragment;
import com.zadtek.shopmall.bean.NearbyItemBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.LocationManger;
import com.zadtek.shopmall.view.SpacesItemDecoration;

import org.jetbrains.annotations.NotNull;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zack on 2018/5/31.
 */

public class NearbyFragment extends AbsFragment {
    private RecyclerView recyclerView;
    private NearbyAdapter nearbyAdapter;
    private List<NearbyItemBean> data = new LinkedList<>();
    private int page = 1;
    private int pagesize = 10;

    @Override
    public int setView() {
        return R.layout.fragment_nearby;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
        if(page == 1){
            showLoadingProgress();
        }
        Map<String, Object> param = NetTool.newParams();
        param.put("type",1);
        param.put("address", LocationManger.location.getCity());
        param.put("page", page);
        param.put("pagesize", pagesize);
        NetTool.getApi().getCarInfo(param).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<NearbyItemBean>>>() {
                    @Override
                    public void onData(BaseResponse<List<NearbyItemBean>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().size()!=0){
                                data.addAll(value.getData());
                                page++;
                                nearbyAdapter.notifyDataSetChanged();
                                nearbyAdapter.loadMoreComplete();
                            }else{
                                nearbyAdapter.loadMoreEnd();
                            }
                        }
                        dismissLoadingProgress();
                    }
                });
    }

    private void initLogic() {
        MultiPictureView.setImageLoader(new ImageLoader() {
            @Override
            public void loadImage(@NotNull ImageView image, @NotNull Uri uri) {
                Glide.with(mActivity).load(uri.getPath().substring(1)).placeholder(R.drawable.placeholder).into(image);

            }
        });
        nearbyAdapter = new NearbyAdapter(R.layout.nearby_item,data);

        nearbyAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                requestData();
            }
        }, recyclerView);
        nearbyAdapter.setEmptyView(R.layout.normal_empty_view);
        recyclerView.setAdapter(nearbyAdapter);
        nearbyAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.iv_call:
                        call(data.get(position).getPhone());
                        break;
                }
            }
        });
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(new SpacesItemDecoration(20));
        recyclerView.setLayoutManager(linearLayoutManager);

    }

    private class NearbyAdapter extends BaseQuickAdapter<NearbyItemBean,BaseViewHolder> {
        public NearbyAdapter(int layoutResId, @Nullable List<NearbyItemBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, NearbyItemBean item) {
            Glide.with(mActivity).load(item.getUseravatar()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.user_avatar));
            helper.setText(R.id.tv_user_name,item.getName());
            helper.setText(R.id.tv_day,item.getAddtime());
            helper.setText(R.id.tv_content,item.getRemark());
            helper.setText(R.id.tv_destination_info,"想去  "+item.getDestination());
            helper.setText(R.id.tv_distance,item.getDeparture()+",距我"+item.getDistance());
            MultiPictureView multiPictureView = helper.getView(R.id.multi_view);
            multiPictureView.clearItem();
            for(int i=0;i<item.getImage().size();i++){
                Uri uri = new Uri.Builder().appendPath(item.getImage().get(i)).build();
                multiPictureView.addItem(uri);
            }
            helper.addOnClickListener(R.id.iv_call);
        }
    }
    private void call(String phoneNumber) {
        Uri uri = Uri.parse("tel:"+phoneNumber);
        Intent intent = new Intent(Intent.ACTION_CALL,uri);
        startActivity(intent);
    }
}
